import { Component, Input, OnInit } from '@angular/core';
import { MyModalHelper } from '@core/my-modal.helper';
import { Router } from '@angular/router';
import { NzMessageService } from 'ng-zorro-antd';
import { AuditingComponent } from '../../../auditing/auditing.component';
import { LevelApplicationService } from '@core/routes-services/level-application.service';

@Component({
  selector: 'level-application-list',
  templateUrl: './level-application-list.component.html',
})
export class LevelApplicationListComponent implements OnInit {

  isLoading = false;
  keyword: any;
  parentId:any;
  childId:any;
  @Input() status: any = 1;
  @Input() allAgent: any [] = [];
  @Input() levelOptions: any [] = [];
  page: any = { number: 1, size: 10, totalElements: 0, content: [] };


  constructor(private message: NzMessageService,
              private router: Router,
              private levelApplicationService: LevelApplicationService,
              private modalHelper: MyModalHelper) {
  }

  ngOnInit() {
    this.initPage();
  }


  initPage() {
    this.isLoading = true;
    this.levelApplicationService.pagination(this.page, this.keyword,this.status,this.parentId,this.childId).then((res) => {
      if (res.status.toString().startsWith('20')) {
        this.page = res.data;
        ++this.page.number;
      } else {
        this.message.warning(res.message);
      }
      this.isLoading = false;
    }, error => {
      this.message.error(error.message ? error.message : error.error);
      this.isLoading = false;
    });
  }

  /**
   * 搜索重置分页数据
   */
  search() {
    this.page = { number: 0, size: 10, totalElements: 0, content: [] };
    this.initPage();
  }

  changePageNo(pageNo) {
    this.page = { number: pageNo, size: 10, totalElements: 0, content: [] };
    this.initPage();
  }

  agree = (entity) => {
    this.levelApplicationService.upgrade(entity.id,entity.childId, entity.parentId, {status: 2}).then((res)=>{
      if (res.status.toString().startsWith('20')) {
        this.message.warning("操作成功");
        this.changePageNo(this.page.number);
      } else {
        this.message.warning(res.message);
      }
      this.isLoading = false;
    }, error => {
      this.message.error(error.message ? error.message : error.error);
    })
  };

  reject = (entity) => {
    let temp: boolean;
    const bak = {};
    Object.assign(bak, entity);
    this.modalHelper.open(AuditingComponent, { entity: bak,businessService: this.levelApplicationService}, 360, {
      nzTitle: '驳回升级请求',
    }).subscribe(result => {
      if(result){
        this.message.success("驳回成功")
        this.changePageNo(this.page.number);
      }
    }, (error) => {
      console.error('弹窗回调出错：', error);
    });
  };


}
